package com.rsk.service.info.impl;


import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.info.RskInfoReleaseRecordRepository;
import com.rsk.dao.info.RskInformationRepository;
import com.rsk.dao.info.extend.RskInformationExtend;
import com.rsk.dao.utils.SearchDataUtils;
import com.rsk.entity.info.RskInfoReleaseRecord;
import com.rsk.entity.info.RskInformation;
import com.rsk.service.info.RskInformationService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;
import com.rsk.utils.tools.PageUtils;


@Service(interfaceClass = RskInformationService.class)
public class RskInformationServiceImpl implements RskInformationService{
	
	@Autowired
	private RskInformationRepository rskInformationRepository;
	
	@Autowired
	private RskInfoReleaseRecordRepository rskInfoReleaseRecordRepository;
	
    @Autowired
    private RskInformationExtend rskInformationExtend;
    
    @Autowired
    private SearchDataUtils searchDataUtils;

    @Transactional
	public int saveInformation(Long labelId, Integer informationType,
			String informationTitle, String informationPhoto,
			String informationUser, String information, String informationUrl,
			Integer informationSource, Long operatorUser) {
		RskInformation rskInformation = new RskInformation();
		rskInformation.setId(SnowflakeIdWorker.generateId(true));
		rskInformation.setLabelId(labelId);
		rskInformation.setInformationTitle(informationTitle);
		rskInformation.setInformationPhoto(informationPhoto);
		rskInformation.setInformationUser(informationUser);
		rskInformation.setInformationRead(0);
		rskInformation.setIsDel(0);
		rskInformation.setInformationStatus(1);
		if(informationType==0){
			rskInformation.setInformation(information);
		}else if(informationType==1){
			rskInformation.setInformationUrl(informationUrl);
		}else{
			return -1;
		}
		rskInformation.setInformationType(informationType);
		rskInformation.setOperatorUser(operatorUser);
		rskInformation.setOperatorTime(new Date());
		rskInformationRepository.saveAndFlush(rskInformation);
		return 1;
	}

	@Transactional
	public int updateInformation(Long id, Integer informationType,String informationTitle, String informationPhoto,
			String informationUser, String information, String informationUrl, Integer informationSource, 
			Long informationRead, Long operatorUser) {
		RskInformation rskInformation = new RskInformation();
		rskInformation.setId(id);
		rskInformation.setInformationTitle(informationTitle);
		rskInformation.setInformationPhoto(informationPhoto);
		rskInformation.setInformationUser(informationUser);
		rskInformation.setInformationStatus(1);
		if(informationType==0){
			rskInformation.setInformation(information);
		}else if(informationType==1){
			rskInformation.setInformationUrl(informationUrl);
		}else{
			return -1;
		}
		rskInformation.setInformationType(informationType);
		rskInformation.setOperatorUser(operatorUser);
		rskInformation.setOperatorTime(new Date());
		rskInformationRepository.saveAndFlush(rskInformation);
		//修改发布表记录
		rskInfoReleaseRecordRepository.delInfoReleaseRecordAll(operatorUser, id);
		RskInfoReleaseRecord rskInfoReleaseRecord = new RskInfoReleaseRecord();
		rskInfoReleaseRecord.setId(SnowflakeIdWorker.generateId(true));
		rskInfoReleaseRecord.setInformationId(id);
		rskInfoReleaseRecord.setInformationStatus(1);
		rskInfoReleaseRecord.setIsDel(0);
		rskInfoReleaseRecord.setOperatorUser(operatorUser);
		rskInfoReleaseRecord.setOperatorTime(new Date());
		rskInfoReleaseRecordRepository.saveAndFlush(rskInfoReleaseRecord);
		return 1;
	}

	@Transactional
	public int delInformation(Long id, Long operatorUser) {
		return rskInformationRepository.delInformation(id, operatorUser);
	}

	@Transactional
	public int updateInformationRead(Long id) {
		return rskInformationRepository.updateInformationRead(id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageUtils<Map> selectInformationByPage(Long labelID,
			String informationTitle, Integer nowPage, Integer pageSize) {
		Map<String, Object> map = rskInformationExtend.selectInfoTabByPage(labelID, informationTitle);
        String sql = map.get("sql").toString();
        String countSQL =  map.get("countSQL").toString();
        Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
        PageUtils<Map> page = searchDataUtils.searchByPage(nowPage, pageSize, sql, countSQL, paramMap, Map.class);
		return page;
	}

	@Transactional
	public int updateInformationStatus(Integer informationStatus, Long id, Long operatorUser) {
		rskInformationRepository.updateInformationStatus(id, informationStatus, operatorUser);
		rskInfoReleaseRecordRepository.delInfoReleaseRecordAll(operatorUser, id);
		RskInfoReleaseRecord rskInfoReleaseRecord = new RskInfoReleaseRecord();
		rskInfoReleaseRecord.setId(SnowflakeIdWorker.generateId(true));
		rskInfoReleaseRecord.setInformationId(id);
		rskInfoReleaseRecord.setInformationStatus(informationStatus);
		rskInfoReleaseRecord.setIsDel(0);
		rskInfoReleaseRecord.setOperatorUser(operatorUser);
		rskInfoReleaseRecord.setOperatorTime(new Date());
		rskInfoReleaseRecordRepository.saveAndFlush(rskInfoReleaseRecord);
		return 1;
	}


}
